package game;

import java.awt.Graphics2D;
import java.awt.Rectangle;
//import java.awt.geom.Point2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.LinkedList;

abstract class BaseLevel extends Scene 
{
	
	protected Player player;
	static protected Respawn respawn;
	public static GameObject Mapas[];
	public BaseMapa mapa;
	public static int inimigosMortos = 0;
	private static int proximoCapacitor;
	public Point sizeMap;
	Radar radar;
	
	float tempo = 0;

	public BaseLevel() 
	{
		super();
		ShootManager.getInstance().ClearTiros();
		Map.GetInstance().CARREGARMAPA();
		Ainteligence.CriarCasasEnemie();
		CapacitorManager.getInstance().Reiniciar();
		CapacitorManager.getInstance().ZerarBaseCapacitor();
		Initialze();
		Inimigo_04.bombs = new LinkedList<Bomb>();
		Explosao.Iniciar();
		radar = new Radar();
		ShootManager.getInstance().enemyShoot = new LinkedList<Shoot>();
		inimigosMortos = 0;
		proximoCapacitor = 0;
	}

	protected void Initialze() 
	{
		
		player = new Player(25,
					new Point(
							(int)(CapacitorManager.getInstance().baseCapacitor.getRectangle().getCenterX() - 20),
							(int)(CapacitorManager.getInstance().baseCapacitor.getRectangle().getCenterY() - 20)),
					new Point(40, 40),
					new Point(0, 0),
					new Point(40, 40),
					new Point(1, 1),
					100f,
					true);
		
		Audios.getInstance().Loop(8);
	}

	public void update(Rectangle[] rectMapa) {
		
		//System.out.println("INIMIGOS MORTOS:  " + BaseLevel.inimigosMortos);
		//System.out.println("PROXIMO CAPACITOR:  " + proximoCapacitor);
		
		tempo += Time.getInstance().getLoopTime();

		for(int i = 0; i< Inimigo_04.bombs.size(); i++)
			Inimigo_04.bombs.get(i).Update(player, rectMapa);
		
		
		if (Player.isDead())
		{
			Audios.getInstance().stopAll();
			//SoundClip.StopAll();
			GameOver.level = SceneManager.getLevel();
			SceneManager.changeScene(9);
		}

		if (Player.isWin())
		{
			Audios.getInstance().stopAll();
			
			if(SceneManager.getLevel() == 5)
			{
				Congrats.pontuacao = 100 + Game.rand.nextInt(1000);
				SceneManager.changeScene(16);
			}
			else
				SceneManager.changeScene(11);
		}

		ShootManager.getInstance().UpdateShoot();

		Rectangle[] rect = new Rectangle[Respawn.enemie.size()];

		for (int i = 0; i < Respawn.enemie.size(); i++) {
			rect[i] = new Rectangle(
					(int) Respawn.enemie.get(i).position.getX(),
					(int) Respawn.enemie.get(i).position.getY(),
					(int) Respawn.enemie.get(i).dimension.x,
					(int) Respawn.enemie.get(i).dimension.y);
		}

		player.Update(rect);

		for (int i = 0; i < rectMapa.length; i++) {
			if (player.ColisaoRect(rectMapa[i])) {
				if (player.posX == 1)
					player.setPosition(new Point2D.Double(rectMapa[i].getMinX()
							- player.getDimension().getX(), player
							.getPosition().getY()));
				else if (player.posX == -1)
					player.setPosition(new Point2D.Double(
							rectMapa[i].getMaxX(), player.getPosition().getY()));
			}
		}

		for (int i = 0; i < rectMapa.length; i++) {
			if (player.ColisaoRect(rectMapa[i])) {
				if (player.posY == 1)
					player.setPosition(new Point2D.Double(player.getPosition()
							.getX(), rectMapa[i].getMinY()
							- player.getDimension().getY()));
				else if (player.posY == -1)
					player.setPosition(new Point2D.Double(player.getPosition()
							.getX(), Mapa_01.getObstaculos()[i].getMaxY()));
			}
		}
		Explosao.Update();
		
		CapacitorManager.getInstance().Update(player);

		
		
		radar.Update(player, rectMapa, Respawn.enemie,  mapa, CapacitorManager.getInstance().baseCapacitor.getRectangle());

	}

	public void draw(Graphics2D g2d) {
	}

	public void Draw2(Graphics2D g2d) {
		
		CapacitorManager.getInstance().Draw(g2d);
		respawn.Draw(g2d);
		player.draw(g2d);
		Explosao.Draw(g2d);
		ShootManager.getInstance().Draw(g2d);
		
		for(int i = 0; i< Inimigo_04.bombs.size(); i++)
			Inimigo_04.bombs.get(i).draw(g2d);
		radar.Draw(g2d, player);
	}

	public static void Remover(int i) {
		Respawn.enemie.remove(i);
		Respawn.ia.remove(i);
	}

	public static void addInimigoMorto(Point pos)
	{
		if(inimigosMortos == 0)
			proximoCapacitor += CapacitorManager.getInstance().minEnimie + Game.rand.nextInt(CapacitorManager.getInstance().maxEnimie - CapacitorManager.getInstance().minEnimie);
		
		inimigosMortos++;
		
		if(inimigosMortos >= proximoCapacitor)
		{
			proximoCapacitor += CapacitorManager.getInstance().minEnimie + Game.rand.nextInt(CapacitorManager.getInstance().maxEnimie - CapacitorManager.getInstance().minEnimie);
			CapacitorManager.getInstance().addCapacitor(pos);
		}
	}

	public Point PosicaoInicialInimigo(Point2D mapaPos, Point sizeMap)
	{
		Point pos = new Point(0,0);
		
		switch(Game.rand.nextInt(4))
		{
			case 0:
				//Criar em Cima
				pos = new Point(
						Game.rand.nextInt((int)this.sizeMap.getX()),
						-100 + (int)mapaPos.getY()
						);
				break;
				
				
			case 1:
				//Criar em BAIXO
				pos = new Point(
						Game.rand.nextInt((int)this.sizeMap.getX()),
						(100 + (int)this.sizeMap.getY()) + (int)mapaPos.getY()
						);
				break;
				
				
			case 2:
				//Criar na ESQUERDA
				pos = new Point(
						-100 + (int)mapaPos.getX(),
						Game.rand.nextInt((int)this.sizeMap.getY())
						);
				break;
				
				
			case 3:
				//Criar na DIREITA
				pos = new Point(	
						(100 + (int)this.sizeMap.getX()) + (int)mapaPos.getX(),
						Game.rand.nextInt((int)this.sizeMap.getY())		
						);
				break;
			
		
		}
		return pos;
		
	}

}


